package com.zx.viewlearn.views.touchEvent;

import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import static android.content.ContentValues.TAG;

/**
 * Created by zhoux on 2018/1/12.
 * 注释：
 */

public class MyViewGroupB extends ViewGroup {
    String TAG = MyViewGroupB.class.getSimpleName();

    public MyViewGroupB(Context context) {
        super(context);
        init();
    }

    public MyViewGroupB(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
//        Log.e("touchEvent", TAG + " dispatchTouchEvent");
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.e("touchEvent", TAG + " onInterceptTouchEvent");
        return super.onInterceptTouchEvent(ev);
    }

    int onMeasureCount = 0;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        onMeasureCount++;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            Log.e("onLayout", TAG + i + " 第" + onMeasureCount + "次measureChild之前： getMeasuredWidth:" + child.getMeasuredWidth());//measureChild方法调用之前 getMeasuredWidth 方法没有值
            measureChild(child, widthMeasureSpec, heightMeasureSpec);//todo measureChild 测量后，getMeasuredWidth getMeasuredHeight才会有值，否则为0
            Log.e("onLayout", TAG + i + " 第" + onMeasureCount + "次measureChild之后： getMeasuredWidth:" + child.getMeasuredWidth());//measureChild方法调用之后 getMeasuredWidth 方法有值
        }

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            Log.e("onLayout", TAG + " l:" + l);
            Log.e("onLayout", TAG + " t:" + t);
            Log.e("onLayout", TAG + " r:" + r);
            Log.e("onLayout", TAG + " b:" + b);
            if (child.getVisibility() != GONE) {
                Log.e("onLayout", TAG + " child.layout之前 getWidth:" + child.getWidth());//layout方法调用之前 getWidth 方法没有值
                Log.e("onLayout", TAG + " child.layout之前 getMeasuredWidth:" + child.getMeasuredWidth());//layout方法调用之前 getMeasuredWidth 方法有值
                child.layout(l, t, child.getMeasuredWidth(), child.getMeasuredHeight());//todo 注意:在layout方法调用前只能用getMeasuredWidth获取宽度
                Log.e("onLayout", TAG + " child.layout之后 getWidth:" + child.getWidth());//layout方法调用后 getWidth 方法才会有值
                Log.e("onLayout", TAG + " child.layout之后 getMeasuredWidth:" + child.getMeasuredWidth());
            }
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e("touchEvent", TAG + " onTouchEvent");
        return true;
        //true 父控件不会执行 onTouchEvent方法
        // 经测试 会多次触发父控件的dispatchTouchEvent  onInterceptTouchEvent 方法，
        // 然后再次触发自己的dispatchTouchEvent  onInterceptTouchEvent onTouchEvent
    }
}
